package com.financing.wap.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.dao.IAuthenticationDao;
import com.financing.wap.dao.IUserDao;
import com.financing.wap.dto.AuthenticationDTO;
import com.financing.wap.dto.CertDTO;
import com.financing.wap.service.IAuthenticationService;

/**
 * Fa认证
 * @author hetaihe
 */
@Service("authenticationService")
public class AuthenticationServiceImpl implements IAuthenticationService {
	
	@Autowired
	public IAuthenticationDao authenticationDao;
	
	@Autowired
	public IUserDao userDao;

	@Override
	public boolean submitAuthInfo(AuthenticationDTO auth) {
		List<CertDTO> certList = auth.getCert();
		
//		authenticationDao.submitAuthInfo(auth);
		authenticationDao.deleteCertInfo(auth.getFaId());
		if(certList != null && certList.size() > 0){
			//循环插入证书
			for(int i=0; i < certList.size(); i++){
				CertDTO cert = certList.get(i);
				if(cert != null){
					cert.setFaId(auth.getFaId());
					//循环插入证书
					authenticationDao.submitCertInfo(cert);
				}
			}
		}
		//插入身份信息
		return authenticationDao.submitAuthInfo(auth);
	}
	
	

	@Override
	public boolean submitCertInfo(CertDTO cert) {
		
		return authenticationDao.submitCertInfo(cert);
	}

	@Override
	public AuthenticationDTO queryAuthInfo(String faId) {
		AuthenticationDTO auth =  authenticationDao.queryAuthInfo(faId);
		if(auth != null ){
			List<CertDTO> certList = (List<CertDTO>) authenticationDao.queryCertList(faId);
			auth.setCert(certList);
		}else{
			auth = new AuthenticationDTO();
			auth.setCert(new ArrayList<CertDTO>());
		}
		return auth;
	}

	@Override
	public List<CertDTO> queryCertList(String faId) {
		return authenticationDao.queryCertList(faId);
	}



	@Override
	public boolean updateAuthInfo(AuthenticationDTO auth) {
//		authenticationDao.deleteCertInfo(auth.getFaId());
		//插入身份信息
//		return authenticationDao.submitAuthInfo(auth);
		if(auth.getId()!=null&&auth.getId()>0){
			return authenticationDao.authenticationUpdate(auth);
		}else{
			return authenticationDao.submitAuthInfo(auth);
		}
	}

}
